/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.icanci.loopstack.damc.core.server.service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import cn.icanci.loopstack.damc.spi.model.refresh.DynamicConditionRequest;
import cn.icanci.loopstack.damc.spi.model.refresh.DynamicConditionResponse;

/**
 * @author icanci
 * @since 1.0 Created in 2025/05/15 17:30
 */
public class DynamicConditionService {
    /**
     * 获取所有的 Condition
     * 
     * @param request request
     * @return DynamicConditionResponse
     * @throws Exception
     */
    public List<DynamicConditionResponse> findConditions(DynamicConditionRequest request) throws Exception {
        List<DynamicConditionResponse> responses = new ArrayList<>();

        List<String> classes = request.getClasses();
        for (String clazz : classes) {
            Class<?> clz = Class.forName(clazz);
            // 获取类的所有方法（包括 public、protected、default 和 private 方法）
            Method[] methods = clz.getDeclaredMethods();

            List<DynamicConditionResponse.Cell> cells = new ArrayList<>();

            DynamicConditionResponse response = new DynamicConditionResponse();
            response.setClazz(clazz);
            response.setCells(cells);

            for (Method method : methods) {
                // 获取方法名
                String methodName = method.getName();
                // 获取方法的描述符
                String descriptor = getMethodDescriptor(method);
                DynamicConditionResponse.Cell cell = new DynamicConditionResponse.Cell();
                cell.setMethodName(methodName);
                cell.setDescriptor(descriptor);
                cells.add(cell);
            }
            responses.add(response);
        }

        // 注册服务
        // 获取方法的代码
        return responses;
    }

    private static String getMethodDescriptor(Method method) {
        StringBuilder descriptor = new StringBuilder();

        // 方法参数的描述符
        descriptor.append("(");
        for (Class<?> paramType : method.getParameterTypes()) {
            descriptor.append(getTypeDescriptor(paramType));
        }
        descriptor.append(")");

        // 方法返回值的描述符
        descriptor.append(getTypeDescriptor(method.getReturnType()));

        return descriptor.toString();
    }

    private static String getTypeDescriptor(Class<?> type) {
        if (type.isPrimitive()) {
            // 基本类型的描述符
            if (type == void.class)
                return "V";
            if (type == int.class)
                return "I";
            if (type == long.class)
                return "J";
            if (type == double.class)
                return "D";
            if (type == float.class)
                return "F";
            if (type == boolean.class)
                return "Z";
            if (type == char.class)
                return "C";
            if (type == byte.class)
                return "B";
            if (type == short.class)
                return "S";
        } else {
            // 引用类型的描述符
            return "L" + type.getName().replace('.', '/') + ";";
        }
        throw new IllegalArgumentException("Unknown type: " + type);
    }

}
